$(function () {
  const form = layui.form
  const layer = layui.layer

  form.verify({
    nickname: function (value) {
      console.log(value)
      if (value.length < 1 || value.length > 6) {
        return '昵称长度在1-6之间!!!'
      }
    },
  })

  information()
  //获取用户信息
  function information() {
    $.ajax({
      method: 'GET',
      url: '/my/userinfo',
      success: function (res) {
        if (res.status !== 0) return layer.msg('获取信息失败！！')
        console.log(res)
        form.val('UserInfo', res.data)
      },
    })
  }

  $('#reset').on('click', function (e) {
    e.preventDefault()
    information()
  })

  $('.layui-form').on('submit', function (e) {
    e.preventDefault()
    $.ajax({
      method: 'POST',
      url: '/my/userinfo',
      data: $(this).serialize(),
      success: function (res) {
        if (res.status !== 0) return layer.msg(res.message)
        layer.msg(res.message)

        // 调用父级的渲染函数
        window.parent.getInfouser()
      },
    })
  })
})
